package com.tct.worktimeboard.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tct.worktimeboard.entity.WorkTask;
import com.tct.worktimeboard.service.IWorkTaskService;
import com.tct.worktimeboard.util.JsonUtil;
import com.tct.worktimeboard.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyongkui
 * @since 2021-02-24
 */
@RestController
@RequestMapping("/work-task")
public class WorkTaskController {
    @Autowired
    IWorkTaskService workTaskService;

    @GetMapping("/getWorkTaskList")
    public JSONObject getWorkTaskList(WorkTask workTask){
        return workTaskService.getWorkTaskList(workTask);
    }

    @PostMapping("/set")
    public JSONObject set(@RequestBody WorkTask workTask){
        int count = workTaskService.count(new QueryWrapper<WorkTask>().eq("task_name", workTask.getTaskName()));
        if(count > 0){
            return JsonUtil.toErrorData("任务已存在");
        }
        workTask.setCreateTime(TimeUtil.getDateTime());
        workTaskService.save(workTask);
        return JsonUtil.toJsonData();
    }

    @PostMapping("/del")
    public JSONObject del(@RequestBody List<Long> ids){
        workTaskService.removeByIds(ids);
        return JsonUtil.toJsonData();
    }

    @PostMapping("/edit")
    public JSONObject edit(@RequestBody WorkTask workTask){
        workTaskService.updateById(workTask);
        return JsonUtil.toJsonData();
    }

}
